﻿using IMDBApi.Contracts;
using IMDBApi.Contracts.Entities;
using IMDBApi.Contracts.Entities.Title.MainDetail;
using IMDBApi.Implementation.Models;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Windows.Forms;

namespace IMDBApi.App.Views
{
    public partial class DetailView : CustomUserControl
    {
        /// <summary>
        /// ...
        /// </summary>
        private MainDetail Detail = null;

        /// <summary>
        /// ...
        /// </summary>
        public MovieFile movieFile { get; set; }

        /// <summary>
        /// ...
        /// </summary>
        public DetailView()
        {
            InitializeComponent();
        }

        /// <summary>
        /// ...
        /// </summary>
        public DetailView(Form parent) : this()
        {
            this.Parent = parent;
        }

        /// <summary>
        /// ...
        /// </summary>
        public DetailView(Form parent, MainDetail detail)
            : this()
        {
            this.Parent = parent;
            this.Detail = detail;
        }

        /// <summary>
        /// ...
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void UserControl_Load(object sender, EventArgs e)
        {
            (this.Parent as MainForm).SetLoader(true);

            new GenericBackgroundWorker().RunAsync<BackgroundWorker, MovieFile, System.Drawing.Image>(QueryDetailsDoWork, this.movieFile, QueryDetailsWorkerCompleted, QueryDetailsWorkerCompletedWithError);
        }

        /// <summary>
        /// ...
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnCancel_Click(object sender, EventArgs e)
        {
            (this.Parent as MainForm).ChangeUserControl(new MainView(this.Parent));
        }

        #region BackgroundWorker Loading

        /// <summary>
        /// ...
        /// </summary>
        /// <param name="outputArgument"></param>
        private void QueryDetailsWorkerCompleted(System.Drawing.Image outputArgument)
        {
            (this.Parent as MainForm).SetLoader(false);

            if (this.Detail != null)
            {
                this.lblHeading.Text = this.Detail.Data.Title;
                this.lblRating.Text = string.Format("Rating: {0}", this.Detail.Data.Rating);
                this.pbImage.Image = outputArgument;
                this.txtDescription.Text = this.Detail.Data.Plot.Outline;

                foreach (DirectorsSummary item in this.Detail.Data.DirectorsSummary)
                    this.txtDirector.Text += item.Name.Names + " ";

                foreach (WritersSummary item in this.Detail.Data.WritersSummary)
                    this.txtWriters.Text += item.Name.Names + " ";

                foreach (CastSummary item in this.Detail.Data.CastSummary)
                    this.txtStars.Text += item.Name.Names + " ";
            }
        }

        /// <summary>
        /// ...
        /// </summary>
        /// <param name="ex"></param>
        private void QueryDetailsWorkerCompletedWithError(Exception ex)
        {
            (this.Parent as MainForm).SetStatus(ex.Message);
        }

        /// <summary>
        /// ...
        /// </summary>
        /// <param name="worker"></param>
        /// <param name="inputArgument"></param>
        /// <param name="e"></param>
        /// <returns></returns>
        private System.Drawing.Image QueryDetailsDoWork(BackgroundWorker worker, MovieFile inputArgument, DoWorkEventArgs e)
        {
            string json = File.ReadAllText(inputArgument.JSONFullname);

            this.Detail = JSONSerializer.Deserialize<MainDetail>(json);

            return Utils.GetImageFromURL(this.Detail.Data.image.Url);
        }

        #endregion
    }
}
